import { EnumBetResult, EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { RecmInfo, RoundInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";

export interface IFailUpMoneyStrategy {
    reverse: boolean;
    /**
     * 0:平推；1：回退到基础;2:循环
     */
    extendWay: number;
    betArray?: number[];
    baseMoney: number;
    groupCount: number;
}
export class FailUpMoneyStrategy implements IMoneyStrategy {
    option: IFailUpMoneyStrategy;
    prevMoney: number = 0;
    currentMoney: number = 0;
    gameStart: boolean = false;
    startIndex: number = 0;
    betIndex: number = 0;
    betArray: number[] = [];
    baseMoney: number = 0;
    betCount: number = 0;
    roundWinCount: number = 0;
    roundFailCount: number = 0;
    prevWinfail: number = 0;
    groupArray: number[] = [];

    constructor(name: string, option?: IFailUpMoneyStrategy | null, start?: boolean) {
        this.strategyName = name;
        if (option) {
            this.option = option;
        }
        else {
            this.option = {
                reverse: false,
                extendWay: 0,
                betArray: [200, 200, 400, 400, 800, 800, 1600, 1600],
                baseMoney: 100,
                groupCount: 2,
            };
        }
        if (start) this.gameStart = true;
        else this.gameStart = false;
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        throw new Error("Method not implemented.");
    }
    resetRecm(): void {
    }
    resetStrategy(): void {
        this.betIndex = 0;
        this.betArray = this.option.betArray ?? [];
        this.baseMoney = this.option.baseMoney;
        this.startIndex = 0;
        this.currentMoney = this.baseMoney;
        this.prevMoney = 0;
        this.betCount = 0;
        this.roundWinCount = 0;
        this.roundFailCount = 0;
        this.prevWinfail = 0;
        this.groupArray = [];
    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let recmV = roundInfo.userRecm;
        if (!this.gameStart) return [EnumBjlResult.U, recmInfo.recmMoney];

        if (recmInfo.summaryInfo.prevResult == EnumBetResult.Success) {
            // this.prevMoney = this.currentMoney;
            // this.currentMoney = this.baseMoney;
            this.roundWinCount++;
        }
        else if (recmInfo.summaryInfo.prevResult == EnumBetResult.Fail) {
            this.roundFailCount++;
        }

        let sm = this.roundWinCount + this.roundFailCount;
        if (sm % this.option.groupCount == 0) {
            if (this.roundWinCount > this.roundFailCount) {
                this.prevMoney = this.currentMoney;
                this.currentMoney = this.baseMoney;
                this.betIndex = 0;
            }
            else if (this.roundWinCount < this.roundFailCount) {
                this.prevMoney = this.currentMoney;
                this.betIndex++;
                if (this.betIndex >= this.betArray.length) {
                    switch (this.option.extendWay) {
                        case 0:
                            this.currentMoney = this.betArray[this.betArray.length - 1]
                            break;
                        case 1:
                            this.currentMoney = this.baseMoney;
                            this.betIndex = 0;
                            break;
                        case 2:
                            this.currentMoney = this.betArray[this.betIndex % this.betArray.length];
                            break;
                    }
                }
                else {
                    this.currentMoney = this.betArray[this.betIndex];
                }
            }
            else {

            }
            this.prevWinfail = this.roundWinCount - this.roundFailCount;
            if (this.roundWinCount > 0 || this.roundFailCount > 0) {
                this.groupArray.push(this.prevWinfail);
            }

            this.roundFailCount = 0;
            this.roundWinCount = 0;
        }
        else {
            this.prevWinfail = this.roundWinCount - this.roundFailCount;
            //this.groupArray.push(this.prevWinfail);
        }

        if (this.currentMoney < this.baseMoney) {
            this.currentMoney = this.baseMoney
        }

        if (recmV == EnumBjlResult.U)
            return [recmV, recmInfo.recmMoney];
        else if (this.option.reverse)
            recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;


        return [recmV, this.currentMoney];
    }
    resetCurrentMoney() {
        this.currentMoney = this.baseMoney;
    }
    stop() {
        this.gameStart = false;
    }
    restart() {
        this.gameStart = true;
    }
    isStart() {
        return this.gameStart;
    }
    strategyName: string;
    debugCompoenent: string = "";
    copyValue(valueObj: any): void {
        let obj = valueObj as FailUpMoneyStrategy;
        if (obj) {
            //复制
            this.prevMoney = obj.prevMoney;
            this.currentMoney = obj.currentMoney;
            this.gameStart = obj.gameStart;
            this.betIndex = obj.betIndex;
            this.betCount = obj.betCount;
            this.roundWinCount = obj.roundWinCount;
            this.roundFailCount = obj.roundFailCount;
            this.prevWinfail = obj.prevWinfail;
            this.groupArray = obj.groupArray;

        }
    }

}